// pages/Cxtj/Cxtj.js
import ajax from '../../utils/api.js';
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      scrollTop:0,
        currentActive:0,
      
        wtsh: false,
        jcsl:false,
        hzulist: [],
        idx:0,
        xiahgd: '下拉刷新更多内容',
        geud: true,//没有更多内容了
        page: 1,//初始页数
        limit: 10,//一页几条
        ddlb:[],

        daohang: [
            {name: "审核管理"},
            {name: "快报审核"},
            {name: "报告审核"},
          ], 
         shehList:[{
            name:'T-2020070191-D-02',
            times:'2021-06-04',
            babh:'闽DB-T13166',
            sh:'编辑',
            zjsh:'整机合格',
            zt:1
        },
        {
            name:'T-2020070191-D-02',
            times:'2021-06-04',
            babh:'闽DB-T13166',
            sh:'编辑',
            zjsh:'整机合格',
            zt:2
        },
        
    ],
    b_machinery_spec:[],
    // 检测报告的选项卡权限与页面接口名称
    qxsx:[
      { id:'24',
        list: [
        //   { name:'委托检测',ajax:'wetolb',},//api/tEntrustCheck/findCheckApplyList
          { name:'快报信息',ajax:'janceList',},//api/tCheckDaily/findTCheckDailyList/1
          { name:'报告信息',ajax:'yishouli',},//api/tCheckReport/findTCheckReportListPage
        ]
      },
      {id:'101',
        list: [
          { name:'审核管理',ajax:'gongdan',},//api/JygdController/findWorkOrderList
          { name:'原始记录',ajax:'yuansjl2',},//api/tSourceRecord/getSourceRecordCheckList
          { name:'报告审核',ajax:'yishouli',},//api/tCheckReport/findTCheckReportListPage
        ]
      },
      {id:'102',
      list: [
        { name:'审核管理',ajax:'gongdan',},//api/JygdController/findWorkOrderList
        { name:'原始记录',ajax:'yuansjl2',},//api/tSourceRecord/getSourceRecordCheckList
        { name:'报告审核',ajax:'yishouli',},//api/tCheckReport/findTCheckReportListPage
      ]
    },
      {id:'103',
        list: [
          { name:'任务管理',ajax:'Jcrwlb',},//api/checkTask/findCheckTask
          { name:'原始记录',ajax:'yuansjl2',},//api/tSourceRecord/getSourceRecordCheckList
          { name:'报告任务',ajax:'janceList',},//api/tCheckDaily/findTCheckDailyList/1
          { name:'快报任务',ajax:'yishouli',},//api/tCheckReport/findTCheckReportListPage
        ]
      },
      {id:'104',
        list: [
          { name:'任务管理',ajax:'Jcrwlb',},//api/checkTask/findCheckTask
          { name:'原始记录',ajax:'yuansjl2',},//api/tSourceRecord/getSourceRecordCheckList
          { name:'报告列表',ajax:'janceList',},//api/tCheckDaily/findTCheckDailyList/1
          { name:'快报任务',ajax:'yishouli',},//api/tCheckReport/findTCheckReportListPage
        ]
      },
      {id:'105',
        list: [
          { name:'审核管理',ajax:'yuansjl2',},//api/tSourceRecord/getSourceRecordCheckList
          { name:'快报审核',ajax:'janceList',},//api/tCheckDaily/findTCheckDailyList/1
          { name:'报告审核',ajax:'yishouli',},//api/tCheckReport/findTCheckReportListPage
        ]
      },
      {id:'2',
        list: [
          { name:'检测审核',ajax:'wetolb',},//api/tEntrustCheck/findCheckApplyList
          { name:'快报审核',ajax:'janceList',},//api/tCheckDaily/findTCheckDailyList/1
          { name:'报告审核',ajax:'yishouli',},//api/tCheckReport/findTCheckReportListPage
        ]
      },
    ]
    },
    showUserInfo() {
        // 获取本地存储
        this.setData({
            userinfo: wx.getStorageSync('userinfo'),
        })
        // this.getData({
        //     if () {
                
        //     }
        //     userinfo: wx.getStorageSync('userinfo'),
        // })
    },
        // 预览报告 item
        showPdf(e){
            var _this = this
            let paramsObj = {
                checkId:e.currentTarget.dataset.id,
                reportId:e.currentTarget.dataset.reportid
            }
            ajax.findReportSignature(paramsObj).then(res=>{
                let url = res.data.signatureName
                if(url){
                    url = url.replace('http://47.96.148.109:8087/', 'https://test.xmjchyxh.com:8090/')
                    //打开pdf文件
               wx.showLoading({
                   title: '加载中...',
                 })
                 wx.downloadFile({
                   url: url,
                   success: function (res) {
                     const filePath = res.tempFilePath
                     wx.openDocument({
                       filePath: filePath,
                       showMenu: true,
                       fileType: 'pdf', // 指定文件类型
                       success: function (res) {
                         wx.hideLoading()
                       },
                       fail: function (res) {
                         wx.showToast({
                             title: '打开失败！',
                             icon: 'none',
                             duration: 2000
                         })
                       },
                     })
                   },
                   fail: function (res) {
                     wx.showToast({
                         title: '预览失败！',
                         icon: 'none',
                         duration: 2000
                     })
                   }
                 })

                 
                }
            })
        },
    //获取委托检测类型总数
    headzs(){ 
        var _this=this
        ajax.huoquzs({}).then((res)=>{
      
                _this.setData({
                    chkType00Count:res.data.chkType00Count,
                    chkType01Count:res.data.chkType01Count,
                    chkType02Count:res.data.chkType02Count
                  })    
                // wx.setStorageSync('token', res.data.loginToken)
            } ).catch((err) => {
              });  
    },
    go_Jcbgzljcxm(e){
      let id=e.currentTarget.dataset.detailid;
      wx.navigateTo({
          url: `/pages/Jcbgzljcxm/Jcbgzljcxm?id=${id}`,
        })
  },
  goto_Fjshenhe(e){
    let id=e.currentTarget.dataset.detailid;
    wx.navigateTo({
        url: `/pages/Fjshenhe/Fjshenhe?id=${id}`
        })
},
  go_Jckbjcxx(e){
    let id=e.currentTarget.dataset.detailid;
    wx.navigateTo({
        url: `/pages/Jckbjcxx/Jckbjcxx?id=${id}`,
      })
},
goto_Jcsls(e){
  let id=e.currentTarget.dataset.detailid.id;
  wx.navigateTo({
      url: `/pages/Jcsls/Jcsls?id=${id}&urlObjshenhe=${JSON.stringify(e.currentTarget.dataset.detailid)}` 
      })
 
},
// 任务管理 接受函数
jies:function(e){
    var _this = this;
    wx.showModal({
      title: '提示',
      content: '确认是否接受',
      success(res) {
        if (res.confirm) {
          ajax.jies({
            id:e.currentTarget.dataset.id,
          }).then((res) => {
            _this.chonxjz()
          }).catch((err) => {
          });
        } else if (res.cancel) {
        }
      }
    })
  },
// 任务管理 处理函数
goto_Ksjc(e){
    let id = e.currentTarget.dataset.id;
    if(!id){
        return
    }
      ajax.getSourceRecordStatus(id).then((res)=>{
            if(res && res.data && res.data.length && res.data[0] && res.data[0].status && res.data[0].status >= 2){
                wx.showToast({
                    title: '记录已提交，不可编辑！',
                    icon: 'none',
                    duration: 2000
                })
                return
              }
              let indexShenhe = e.currentTarget.dataset.index;
              var urlObjshenhe = JSON.stringify(this.data.ddlb[indexShenhe])
              wx.navigateTo({
                  url: `/pages/Ksjc/Ksjc?id=${id}&urlObjshenhe=${urlObjshenhe}`
                  })
          } ).catch((err) => {
            });
  },
//   获取检测类型 //疑似废弃
    jaclx(value){
        ajax.jaclx({
          typeId:value
        }).then((res) => {
          
        }).catch((err) => {
        });
      },
    //   待办事项
      daibsx(){
        ajax.daibsx({
        }).then((res) => {
          this.setData({
            daibsx: res.data,
          })   
        }).catch((err) => {
        });
      },
    //   我的任务切换选项卡
    switchNav: function (e){
        var cur = e.currentTarget.dataset.current;                
        this.setData({
            idx: cur,
        })      
        this.chonxjz()
    },
    go_dbsy(){
        wx.navigateTo({
            url: '/pages/Dbsy/Dbsy'
          })
    },
    // 原委托审核
    // go_wtsh(){
    //     wx.navigateTo({
    //         url: '/pages/Wtsh/Wtsh'
    //       })
    // },
    // 变更审核
    go_wtsh(){
      wx.navigateTo({
          url: '/pages/DetectChange/DetectChange'
        })
  },
    go_jcsl(){
        wx.navigateTo({
            url: '/pages/Jcsl/Jcsl'
          })
    },
     go_cxtj(){
        wx.navigateTo({
            url: '/pages/Cxtj/Cxtj'
          })
    },
    go_xxgl(){
        wx.navigateTo({
            url: '/pages/Sbxmgl/Sbxmgl'
          })
    },
    go_Jcrwlb(){
        wx.navigateTo({
            url: '/pages/Jcrwlb/Jcrwlb'
          })
    },
    go_Ksjc(){
        wx.navigateTo({
            url: '/pages/Ksjc/Ksjc'
          })
    },
    go_Login(){
        wx.navigateTo({
            url: '/pages/Login/Login'
          })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      
    },
    quanx() {
        //  获取用户权限信息
      ajax.yhqx({}).then( res=>{
         this.lbxr(res.data)
        //  permissions 用户权限 控制 委托审核与检测受理
         var permissions=res.data.permissions
         var wtsh=false //变更审核
         var jcsl=false
         var root=false
         let jcslList = [
            "system:checkReport:list",
            "original:generate:list",
            "original:examine:list",
        ]
        // 检测报告
         for(var i=0;i<permissions.length;i++){
           //变更审核
            if(permissions[i]=='changeapply:checkdate:list'||permissions[i]=='changeapply:checkpersonnel:list'){
              wtsh=true
            }
            if( jcslList.includes(permissions[i])){
                jcsl=true
            }
            if(permissions[i]=='*:*:*'){
              root=true
            }
         }
          this.setData({
            wtsh: root?true:wtsh,
            jcsl:root?true:jcsl,
      
          })    
      }) .catch((err) => {
      }); 
    },
    // 根据角色id筛选选项卡
    lbxr(data){
      var qxsx=this.data.qxsx
      var hzulist=[]
      var sdij=false
      bbq:
      for(var i=0;i<data.roles.length;i++){
        // for(var j=0;j<qxsx.length;j++){
        //    if(data.roles[i].roleid==qxsx[j].id){
        //     for(var z=0;z<qxsx[j].list.length;z++){
        //       hzulist.push(qxsx[j].list[z])
        //     }
        //   }
        // }
       for(var j=0;j<qxsx.length;j++){
          if(data.roles[i].roleid==qxsx[j].id){
            hzulist=qxsx[j].list
            break bbq; 
          } 
         
        }
        if(data.roles[i].roleid=='9999'){
          sdij=true
        }
      }
      if(sdij){
        hzulist=qxsx[0].list
      }
      this.setData({
        hzulist: hzulist,
      })   
      this.chonxjz()
    },
    // 初始化 切换选项卡 点击接受后刷新函数
    chonxjz:function(ajaxs){
      var _this = this;
      _this.setData({
        xiahgd: '下拉刷新更多内容',
        geud: true,//没有更多内容了
        page: 1,//初始页数
        limit: 10,//一页几条
        ddlb:[],
      })
    // 更新函数
      _this.cplist()
  },
//   更新函数
  cplist:function(){
    var _this = this;
      var hzulist=_this.data.hzulist
      var idx=_this.data.idx

      let paramsObj = { }
      if(hzulist[idx].ajax==='yishouli'){
        paramsObj = {
            page: _this.data.page,
            pageSize:_this.data.limit,
            isPrintDefaultLimit: '领导签章'
          }
        }else{
            paramsObj = {
                page: _this.data.page,
                pageSize:_this.data.limit,
              } 
        }
      ajax[hzulist[idx].ajax](paramsObj).then((res) => {
        var ddlb = [];
        var ddlb = new Array();
        ddlb = _this.data.ddlb
        
        for (var i = 0; i < res.data.content.length; i++) {
          ddlb.push(res.data.content[i])
        }
        
        _this.setData({
          ddlb: ddlb,
        })
          if (_this.data.page>=res.data.totalPages) {
          _this.setData({
            geud: false,
            xiahgd: '没有更多了'
          })
        }
     
      }).catch((err) => {
      }) .finally(() => {
        // wx.pageScrollTo({
        //   scrollTop: _this.scrollTop>=330?330:_this.scrollTop, // 滚动到的位置（距离顶部 px）
        //   duration: 300
        // });
      });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      
        this.headzs()   
        this.showUserInfo()
        this.daibsx()
        this.quanx()   
        var _this = this;
        setTimeout(function () {
          _this.setData({
              b_machinery_spec:app.globalData.b_machinery_spec,
          })
        }, 500);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      var _this = this;
      if (_this.data.geud) {
      var page = _this.data.page++;
      page++
      _this.setData({
          page: page
      });
      _this.cplist()
      } else {
      _this.setData({
          xiahgd: '没有更多了'
      });
      }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //监听页面滚动
    onPageScroll:function(e){
      var _this = this;
      _this.setData({
        scrollTop: e.scrollTop
    })
  },

})